package com.lagou.edu.utils;

import java.io.File;
import java.util.List;

public class CustomUtil {

    public static final String POINT = ".";
    public static final String CLASS_PATH ="classpath:";
    public static final String POINT_CLASS = ".class";
    public static final String SLASH = "/";
    public static final String CONTROLLER_SECURITY = "controllerSecurity";
    public static final String METHOD_SECURITY = "methodSecurity";
    public static final String USERNAME = "username";

    /**
     * 解析扫描路径
     * @param dir           文件
     * @param scanPackage   包路径
     * @param classNames    类路径列表
     * @throws Exception    异常
     */
    public static  void listDirectory(File dir, String scanPackage, List<String> classNames) throws Exception {
        File[] files = dir.listFiles();
        if (files != null &&files.length != 0) {
            for (File file : files) {
                if (file.isDirectory()) {
                    listDirectory(file, scanPackage + POINT +file.getName(), classNames);
                } else {
                    // 获取类路径
                    String classPath = scanPackage + POINT + file.getName().replace(POINT_CLASS, "");
                    classNames.add(classPath);
                }
            }
        }

    }

    /**
     * 首字母转小写
     * @param name 类名
     * @return     类名(首字母小写)
     */
    public static String changeLower(String name) {
        return Character.toLowerCase(name.charAt(0)) + name.substring(1);
    }

    /**
     * 判断是否是基本类型
     * @param obj
     * @return    true 是基本类型
     */
    public static boolean checkType(Object obj) {
        try {
            if (obj instanceof String) {
                return true;
            } else if (obj instanceof Number) {
                return true;
            } else if (((Class) obj.getClass().getField("TYPE").get(null)).isPrimitive()) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 权限校验
     * @param security   权限信息
     * @param usernames  请求用户
     * @return true:校验通过  false:校验不通过
     */
    public static boolean checkSecurity(List<String> security, String[] usernames) {
        boolean flag = false;
        if (security != null && security.size()> 0) {
            if (usernames != null && usernames.length > 0){
                String username = usernames[0];
                if (security.contains(username)){
                    flag = true;
                }
            }
        } else {
            flag = true;
        }
        return flag;
    }

}
